package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.SetmealDao;
import com.itheima.reggie.dao.SetmealDishDao;
import com.itheima.reggie.domain.*;

import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.CategoryService;

import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.exception.CustomException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private SetmealDishDao setmealDishDao;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishDao dishDao;

    //添加套餐
    @Override
    public void addSetmeal(SetmealDto setmealDto) {
       try{
           //将菜品存入数据库产生id
           setmealDao.insert(setmealDto);
           //获取dishDto中的id
           Long id = setmealDto.getId();
           //将id存入DishFlavor属性中
           List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
           for (SetmealDish setmealDishe : setmealDishes) {
               setmealDishe.setSetmealId(id);
               setmealDishDao.insert(setmealDishe);
           }
       }catch (Exception c){
           throw new CustomException("该套餐已存在,请重新输入");
       }
    }

    //套餐分页查询
    @Override
    public Page<Setmeal> findPage(int page, int pageSize,String name) {


        Page<Setmeal> p = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        lqw.like(StringUtils.isNotBlank(name),Setmeal::getName,name);

        Page<Setmeal> setmealPage = setmealDao.selectPage(p, lqw);

        //将分页查询查到的套餐数据拿出来
        List<Setmeal> records = setmealPage.getRecords();
        //遍历
        for (Setmeal record : records) {
            //根据CategoryId获取菜品名称
            Long categoryId = record.getCategoryId();

            Category category = categoryService.getById(categoryId);


            record.setCategoryName(category.getName());
        }

        return setmealPage;
    }

    //单个删除和批量删除套餐
    //得考虑两种情况,起售时不允许删除,停售时才能删除
    @Override
    public void deleteIds(Long[] ids) {



        //LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        //获取要删除的套餐
        List<Setmeal> setmeals = setmealDao.selectBatchIds(Arrays.asList(ids));
        //遍历
        for (Setmeal setmeal : setmeals) {
            //获取状态值
            Integer status = setmeal.getStatus();
            //判断状态,起售状态不让删除
            if (status==1){
                throw new CustomException("该套餐在售卖中不允许删除");
            }
            setmealDao.deleteById(setmeal.getId());
        }
        // setmealDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public SetmealDto selectById(Long id) {

        Setmeal setmeal = setmealDao.selectById(id);

        SetmealDto setmealDto = new SetmealDto();

        //BeanUtils.copyProperties()方法的作用是将dish中的数据拷贝到dishDto中
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();

        lqw.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishes = setmealDishDao.selectList(lqw);

        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;


    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {

        //将setmealDto中setmeal的数据添加到数据库

        setmealDao.updateById(setmealDto);

        //将数据库原有的菜品信息删除
        //根据id删除套餐数据库中包含的菜品
        //delete from setmeal_dish where setmeal_id=id
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishDao.delete(lqw);

        //将菜品信息提取出来,添加到数据库中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }

        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    public List<Setmeal> findStmeals(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealDao.selectList(queryWrapper);

        return list;
    }


    //前台页面套餐详情
    @Override
    public List<SetmealDish> getDishs(Long id) {


        LambdaQueryWrapper<SetmealDish> lqw3 = new LambdaQueryWrapper<>();

        lqw3.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(lqw3);

        for (SetmealDish setmealDish : setmealDishes) {
            //select imge from dish where dish_id = id
            LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Dish::getId,setmealDish.getDishId());

            Dish dish = dishDao.selectOne(lqw);
            setmealDish.setImage(dish.getImage());
        }


        return setmealDishes;
    }
}
